<template>
  <div class="product">
    <van-tabs v-model="active">
      <!-- 箭头 -->
      <img
        @click="onClickLeft"
        style="position: absolute;left: 13px;top: 12px;z-index: 999;width: 20px;height: 20px;"
        src="../../../src/assets/images/icon-arrow-right.png"
        alt
      >
      <van-tab>
        <div slot="title">
          <div class="tabs">
            <div style class="tabs-item">持有中</div>
          </div>
        </div>
        <div class="content">
          <!-- 图片 -->
          <div id="images">
            <div class="bi1">
              <div>
                <p>在投资产 {{coinNow}}</p>

                <p>{{investmentAmount}} USDT</p>
              </div>
            </div>
            <div style=" position: absolute;right:35px;">
              <button @click="show=!show">{{coinNow}}估值</button>
            </div>
          </div>
          <!-- 币种选择 -->
          <van-actionsheet v-model="show" :actions="actions" @select="onSelect"/>
          <!-- 待收收益 -->
          <div class="shouyi">
            <van-row>
              <van-col span="6" style>待收收益</van-col>
            </van-row>
          </div>
          <div class="xian"></div>

          <div class="bi">
            <!-- BTC -->

            <div style="width:25%" class="fl iconb">
              <van-icon>
                <img src="../../../src/assets/images/icon-btc@2x.png" alt>
              </van-icon>
            </div>

            <div>
              <p>{{structure}}</p>
              <p>{{parseFloat(moreMoneyBtc).toFixed(4)}}</p>
            </div>

            <!-- ETH -->
            <div style="width:25%" class="fl iconb">
              <van-icon>
                <img src="../../../src/assets/images/icon-eth@2x.png" alt>
              </van-icon>
            </div>
            <div>
              <p>{{coin}}</p>
              <p>{{parseFloat(moreMoneyEth).toFixed(4)}}</p>
            </div>

            <!-- USDT -->
          </div>
          <div class="bi1">
            <div style="width:25%" class="fl iconb">
              <van-icon>
                <img src="../../../src/assets/images/icon-usdt@2x.png" alt>
              </van-icon>
            </div>
            <div>
              <p>{{income}}</p>
              <p>{{parseFloat(moreMoneyUsdt).toFixed(4)}}</p>
            </div>
          </div>
          <div style="height:8px;" class="xian"></div>
          <!-- 暂无相关记录 -->
          <div v-show="dataList.length==0">
            <img
              style="width:50px;height：50px;margin-top:110px"
              src="../../../src/assets/images/icon-record-gray@2x.png"
              alt
            >
            <li
              style="font-size:14px;font-family:PingFang-SC-Regular;font-weight:400;color:rgba(102,102,102,1);"
            >暂无相关记录</li>
            <button @click="product" style="margin-top:15px;">浏览产品</button>
          </div>

          <!-- 记录 -->
          <div v-show="dataList.length!=0">
            <!-- 已购产品 -->
            <div class="shouyi">
              <van-row>
                <van-col span="6" style>已购产品</van-col>
              </van-row>
            </div>
            <div class="xian"></div>
          </div>
          <!-- 列表循环 -->
          <div class="tabs-list">
            <ul>
              <li v-for="item in dataList" :key="item.index">
                <router-link :to="{name: ''}" tag="li" class="list-item">
                  <div class="item-title">{{item.lockedDays}}日{{item.planName}}</div>
                  <div class="flex item-box">
                    <div class="item-l">
                      <div class="num-big">
                        {{item.annualInterestRate}}
                        <span>%</span>
                      </div>
                      <div class="word">预期年化利率</div>
                    </div>
                    <div class="item-r">
                      <div class="num-big">
                        {{item.lockedDays}}
                        <span>天</span>
                      </div>
                      <div class="word">已购：{{item.investmentAmount}}{{item.planName}}</div>
                    </div>
                  </div>
                </router-link>
                <div
                  style="width:100%; background: rgba(239, 239, 239, 1);height:8px;margin-top:17px;"
                ></div>
              </li>
            </ul>
          </div>
          <p
            v-show="dataList.length!=0"
            v-if="flagBtm"
            style="font-size: 10px; color: rgb(204, 204, 204); margin: 10px 0;"
          >没有数据了！！！</p>
          <!-- </div> -->
        </div>
      </van-tab>

      <!-- 已结束 -->
      <van-tab>
        <div slot="title">
          <div class="tabs">
            <div style class="tabs-item">已结束</div>
          </div>
        </div>
        <div class="content">
          <!-- 待收收益 -->

          <div class="shouyi">
            <van-row>
              <van-col span="6" style>已得收益</van-col>
            </van-row>
          </div>
          <div class="xian"></div>

          <div class="bi">
            <!-- BTC -->

            <div style="width:25%" class="fl iconb">
              <van-icon>
                <img src="../../../src/assets/images/icon-btc@2x.png" alt>
              </van-icon>
            </div>

            <div>
              <p>{{structure}}</p>
              <p>{{parseFloat(allMoneyBtc).toFixed(4)}}</p>
            </div>

            <!-- ETH -->
            <div style="width:25%" class="fl iconb">
              <van-icon>
                <img src="../../../src/assets/images/icon-eth@2x.png" alt>
              </van-icon>
            </div>
            <div>
              <p>{{coin}}</p>
              <p>{{parseFloat(allMoneyEth).toFixed(4)}}</p>
            </div>

            <!-- USDT -->
          </div>
          <div class="bi1">
            <div style="width:25%" class="fl iconb">
              <van-icon>
                <img src="../../../src/assets/images/icon-usdt@2x.png" alt>
              </van-icon>
            </div>
            <div>
              <p>{{income}}</p>
              <p>{{parseFloat(allMoneyUsdt).toFixed(4)}}</p>
            </div>
          </div>
          <div style="height:8px;" class="xian"></div>
          <!-- 暂无相关记录 -->
          <div v-show="dataUide.length==0">
            <img
              style="width:50px;height：50px;margin-top:110px"
              src="../../../src/assets/images/icon-record-gray@2x.png"
              alt
            >
            <li
              style="font-size:14px;font-family:PingFang-SC-Regular;font-weight:400;color:rgba(102,102,102,1);"
            >暂无相关记录</li>
            <button @click="product" style="margin-top:15px;">浏览产品</button>
          </div>

          <!-- 记录 -->
          <div v-show="dataUide.length!=0">
            <!-- 已购产品 -->
            <div class="shouyi">
              <van-row>
                <van-col span="6" style>已购产品</van-col>
              </van-row>
            </div>
            <div class="xian"></div>
          </div>
          <!-- 列表循环 -->
          <div class="tabs-list">
            <ul>
              <li v-for="item in dataUide" :key="item.index">
                <router-link style="border:none" :to="{name: ''}" tag="li" class="list-item">
                  <div class="item-title">{{item.lockedDays}}日{{item.planName}}</div>
                  <div class="flex item-box">
                    <div class="item-l">
                      <div class="num-big">
                        {{item.annualInterestRate}}
                        <span>%</span>
                      </div>
                      <div class="word">预期年化利率</div>
                    </div>
                    <div class="item-r">
                      <div class="num-big">
                        {{item.lockedDays}}
                        <span>天</span>
                      </div>
                      <div
                        style="margin-left: -70px;"
                        class="word"
                      >已购：{{item.investmentAmount}}{{item.planName}} 收益：{{item.investmentAmount}}{{item.planName}}</div>
                    </div>
                  </div>
                </router-link>
                <div style="height:8px;" class="xian"></div>
              </li>
            </ul>
          </div>
          <p
            v-show="dataUide.length!=0"
            v-if="flagBtm"
            style="font-size: 10px; color: rgb(204, 204, 204); margin: 10px 0;"
          >没有数据了！！！</p>
          <!-- </div> -->
        </div>
      </van-tab>
    </van-tabs>
  </div>
</template>
<script>
import Vue from "vue";
import axios from "axios";
import { NavBar, Tab, Tabs, Row, Col, PullRefresh, Icon } from "vant";
import {
  totalIncomeAndWaitIncome,
  coinType,
  coinAssets,
  listHold,
  huoBiApi,
  biConversion,
  totalIncomeAndWaitIncomeNum
} from "../../axios/api.js";
export default {
  components: {
    [NavBar.name]: NavBar,
    [Tab.name]: Tab,
    [Tabs.name]: Tabs,
    [Row.name]: Row,
    [Col.name]: Col,
    [PullRefresh.name]: PullRefresh,
    [Icon.name]: Icon
  },
  data() {
    return {
      moreMoneyBtc: "",
      moreMoneyEth: "",
      moreMoneyUsdt: "",
      allMoneyBtc: "",
      allMoneyEth: "",
      allMoneyUsdt: "",
      pages: 1,
      loading: false,
      finished: false,
      flagBtm: false,
      investmentAmount: 0,
      waitIncome: "",
      grandTotalIncome: "",
      data: "",
      count: 0,
      isLoading: false,
      active: 0,
      dataList: [],
      dataUide: [],
      coinNow: "",
      coinNowId: "234314", //btc
      show: false,
      actions: [],
      allMoney: "", //已得收益
      moreMoney: "", //代收收益
      income: "234313", //USDT
      coin: "234516", //ETH
      structure: "234314"
    };
  },
  methods: {
    //返回上一页
    onClickLeft() {
      this.$router.push("/mine");
    },
    product() {
      this.$router.push("/product");
    },

    //获取在投资产
    getLostMoney() {
      totalIncomeAndWaitIncomeNum({
        params: {
          token: localStorage.getItem("setToken"),
          virtualWalletId: this.coinNowId
        }
      }).then(res => {
        if (res.data == null) {
          this.investmentAmount = "0";
        } else {
          this.investmentAmount = res.data.data.investmentAmount;
        }
        if (res.data.data.investmentAmount == null) {
          this.investmentAmount = "0";
        } else {
          this.investmentAmount = res.data.data.investmentAmount;
        }
        console.log(this.investmentAmount, 123456);

        //转换
        biConversion({
          params: {
            presentAmount: this.investmentAmount,
            virtualWallteId: this.coinNowId
          }
        }).then(res => {
          console.log(res.data);
          this.investmentAmount = res.data.data;
        });
      });
    },

    // 币种菜单

    onSelect(item) {
      // 点击选项时默认不会关闭菜单，可以手动关闭
      this.show = false;
      console.log(item);
      // btc
      (this.coinNow = item.name),
        (this.coinNowId = item.id),
        this.getLostMoney(this.coinNowId);

      this.getIncome();

      console.log(this.coinNowId);
    },

    getCoinType() {
      //获取币分类列表
      var that = this;
      coinType()
        .then(res => {
          console.log(res.data);
          that.coinType = res.data.data;
          that.actions = res.data.data;

          for (var i = 0; i < res.data.data.length; i++) {
            (that.actions[i].name = res.data.data[i].virtual_wallet_name),
              (that.actions[i].dataId = i);
          }
          // btc
          that.coinNow = that.actions[1].name;
          that.coinNowId = that.actions[1].id;
          // eth
          that.coin = that.actions[2].name;
          that.coinId = that.actions[2].id;
          // usdt
          that.income = that.actions[0].name;
          that.incomeId = that.actions[0].id;

          that.structure = that.actions[1].name;
          that.structureId = that.actions[1].id;

          that.getIncome();
          // eth
          that.getIncome1();
          // USDT
          that.getIncome2();
        })
        .catch(function(err) {});
    },
    // 持有中 已结束接口
    hold() {
      var that = this;
      that.loading = true;
      listHold({
        params: {
          token: localStorage.getItem("setToken"),
          page: that.pages,
          rows: 8,
          type: 0
        }
      }).then(res => {
        console.log(res.data.data);
        if (res.data.data.length == 0) {
          that.flagBtm = true;
          return false;
        } else {
          that.dataList = res.data.data;
        }
      });
    },
    //上拉加载
    downUp() {
      var that = this;
      var startX, startY, endX, endY, X, Y;
      $("body")
        .on("touchstart", function(e) {
          var touch = e.originalEvent.targetTouches[0];
          startX = touch.pageX;
          startY = touch.pageY;
        })
        .on("touchend", function(e) {
          var touch = e.originalEvent.changedTouches[0];
          endX = touch.pageX;
          endY = touch.pageY;
          X = endX - startX;
          Y = endY - startY;
          if (Y < -30) {
            that.pages++;
            console.log(that.pages);
            listHold({
              params: {
                token: localStorage.getItem("setToken"),
                page: that.pages,
                rows: 8,
                type: 0
              }
            }).then(res => {
              if (res.data.data.length <= 0) {
                that.flagBtm = true;
                return false;
              } else {
                for (var i = 0; i < res.data.data.length; i++) {
                  that.dataList.push(res.data.data[i]);
                }
              }
              console.log(that.dataList, 88888);
            });
          }
        });
    },

    over() {
      // 已结束
      var that = this;
      that.loading = true;
      listHold({
        params: {
          token: localStorage.getItem("setToken"),
          page: that.pages,
          rows: 8,
          type: 1
        }
      }).then(res => {
        console.log(res.data.data);
        if (res.data.data.length == 0) {
          that.flagBtm = true;
          return false;
        } else {
          that.dataUide = res.data.data;
        }
      });
    },

    downUp1() {
      var that = this;
      var startX, startY, endX, endY, X, Y;
      $("body")
        .on("touchstart", function(e) {
          var touch = e.originalEvent.targetTouches[0];
          startX = touch.pageX;
          startY = touch.pageY;
        })
        .on("touchend", function(e) {
          var touch = e.originalEvent.changedTouches[0];
          endX = touch.pageX;
          endY = touch.pageY;
          X = endX - startX;
          Y = endY - startY;
          if (Y < -30) {
            that.pages++;
            console.log(that.pages);
            listHold({
              params: {
                token: localStorage.getItem("setToken"),
                page: that.pages,
                rows: 8,
                type: 1
              }
            }).then(res => {
              if (res.data.data.length <= 0) {
                that.flagBtm = true;
                return false;
              } else {
                for (var i = 0; i < res.data.data.length; i++) {
                  that.dataUide.push(res.data.data[i]);
                }
              }
              console.log(that.dataUide, 88888);
            });
          }
        });
    },

    //累计收益
    getIncome() {
      totalIncomeAndWaitIncome({
        params: {
          token: localStorage.getItem("setToken"),
          virtualWalletId: this.structureId
        }
      }).then(res => {
        console.log(res.data, 15696);
        if (res.data.data.grandTotalIncome == null) {
          this.allMoneyBtc = "0";
        } else {
          this.allMoneyBtc = res.data.data.grandTotalIncome;
        }
        if (res.data.data.waitIncome == null) {
          this.moreMoneyBtc = "0";
        } else {
          this.moreMoneyBtc = res.data.data.waitIncome;
        }
      });
    },

    getIncome1() {
      totalIncomeAndWaitIncome({
        params: {
          token: localStorage.getItem("setToken"),
          virtualWalletId: this.coinId
        }
      }).then(res => {
        console.log(res.data, 15696);
        if (res.data.data.grandTotalIncome == null) {
          this.allMoneyEth = "0";
        } else {
          this.allMoneyEth = res.data.data.grandTotalIncome;
        }
        if (res.data.data.waitIncome == null) {
          this.moreMoneyEth = "0";
        } else {
          this.moreMoneyEth = res.data.data.waitIncome;
        }
      });
    },

    // usdt
    getIncome2() {
      totalIncomeAndWaitIncome({
        params: {
          token: localStorage.getItem("setToken"),
          virtualWalletId: this.incomeId
        }
      }).then(res => {
        console.log(res.data, 15696);
        if (res.data.data.grandTotalIncome == null) {
          this.allMoneyUsdt = "0";
        } else {
          this.allMoneyUsdt = res.data.data.grandTotalIncome;
        }
        if (res.data.data.waitIncome == null) {
          this.moreMoneyUsdt = "0";
        } else {
          this.moreMoneyUsdt = res.data.data.waitIncome;
        }
      });
    }
  },
  created() {
    this.getIncome();
    this.getIncome1();
    this.getIncome2();
    this.downUp();
    this.downUp1();
    this.hold();
    this.over();
    this.getCoinType();
    this.getLostMoney();
  },
  watch: {
    //监听路由变化刷新数据
    $route(to, from) {
      this.getCoinType();
      this.getLostMoney();
    }
  }
};
</script>
<style lang="less" scoped>
@import "../../assets/less/variable.less";
/deep/.van-tab {
  color: @base_textColor;
}
/deep/ .van-tabs__line {
  background-color: #ffffff;
}
/deep/ .van-tab--active {
  color: #d1a568;
}
/deep/ .van-tabs__wrap {
  position: relative;
}
/deep/ .van-hairline--bottom::after {
  border-bottom-width: 0;
}
/deep/ .van-hairline--top-bottom::after {
  border-width: 0;
}
/deep/ .van-tabs--line {
  padding-top: 0;
}
/deep/ .tabs-item {
  font-size: 16px;
  font-family: PingFang-SC-Medium;
  font-weight: 500;
}
.product {
  height: 100%;
  background: #ffffff;
  overflow: hidden;
  position: relative;

  #images {
    width: 100%;
    height: 100px;
    background: url(../../../src/assets/images/card-background.png);
    background-size: 100%;
    display: flex;
  }
  #images p {
    font-size: 0.4rem;
    font-family: PingFang-SC-Medium;
    font-weight: 500;
    color: rgba(255, 255, 255, 1);
    padding-top: 10px;
    margin-left: 20px;
    margin-top: 10px;
  }

  button {
    border: 1px solid #d1a568;
    background: none;
    width: 100px;
    height: 30px;
    border-radius: 20px;
    font-size: 0.35rem;
    font-family: PingFang-SC-Medium;
    font-weight: 500;
    color: rgba(209, 165, 104, 1);
    margin-top: 20px;
  }
  //   收益
  .shouyi .van-row {
    font-size: 0.4rem;
    font-family: PingFang-SC-Medium;
    font-weight: 500;
    color: rgba(51, 51, 51, 1);
    margin-top: 17px;
  }
  .xian {
    width: 100%;
    height: 1px;
    background: rgba(239, 239, 239, 1);
    margin-top: 17px;
  }
  .bi {
    display: flex;

    justify-content: flex-start;
  }
  .bi img {
    width: 25px;
    height: 25px;
    padding-top: 15px;
  }
  .bi p {
    font-size: 14px;
    font-family: PingFang-SC-Regular;
    font-weight: 400;
    color: rgba(102, 102, 102, 1);
    text-align: left;
    padding-top: 10px;
  }
  .bi1 {
    display: flex;

    justify-content: flex-start;
  }
  .bi1 img {
    width: 25px;
    height: 25px;
    padding-top: 15px;
  }
  .bi1 p {
    font-size: 14px;
    font-family: PingFang-SC-Regular;
    font-weight: 400;
    color: rgba(102, 102, 102, 1);
    text-align: left;
    padding-top: 10px;
  }

  // 定期分币计划
  .tabs-list {
    margin-top: 10px;
    .list-title {
      text-align: left;
      font-size: @base_textSize;
      font-weight: bold;
      color: @base_textColor;
      // border-bottom: @base_boder;
      background: @base_background;
      padding: 10px;
    }
    .list-item:last-child {
      border-bottom: none;
    }
    .list-item {
      padding-left: 10px;
      background: @base_background;
      // border-bottom: @base_boder;
      .item-title {
        text-align: left;
        padding: 10px;
        font-size: @base_textSize;
        color: @base_textColor;
      }
      .item-box {
        padding-bottom: 10px;
        .item-l {
          width: 50%;
          text-align: left;
          padding-left: 25px;
          .num-big {
            font-size: 0.3rem;
            color: #d1a568;
            span {
              font-size: 0.3rem;
            }
          }
          .word {
            margin-top: 5px;
            color: #969799f5;
          }
        }
        .item-r {
          width: 50%;
          text-align: left;
          padding-left: 25px;
          .num-big {
            font-size: 0.3rem;
            color: @base_textColor;
            span {
              font-size: 0.3rem;
            }
          }
          .word {
            margin-top: 5px;
            color: #333333;
          }
        }
      }
    }
  }
}
</style>
